In [49]:
%pylab
%matplotlib inline 
from scipy import signal
import cv2
import scipy 
from scipy import ndimage


Using matplotlib backend: MacOSX
Populating the interactive namespace from numpy and matplotlib

Convolving a signal


In [4]:
#input signal with noise
x=np.linspace(0,2*np.pi,100, dtype=np.float32)
y=np.sin(x)+0.05*np.random.randn(100)
plt.plot(x,y)
plt.title('Input signal with noise')
plt.axis([0,2*np.pi,-1.5,1.5])
plt.grid()
plt.show()


Moving average filter


In [5]:
filter_average=np.ones(20)

In [9]:
plt.axis([0,100,0.9,1.2])
plt.plot(filter_average, '*')
plt.title('Moving average filter')
plt.show()



In [10]:
smoothened_signal = signal.convolve(y, filter_average, mode='same')/sum(filter_average)

In [13]:
plt.plot(smoothened_signal)
plt.title('Smoothened signal with noise removal')
plt.show()



In [16]:
plt.plot(smoothened_signal, label='Filtered signal')
plt.plot(y, label='Input signal')
plt.legend()
plt.show()


Convolving an Image


In [29]:
img=cv2.imread('myself.jpg',0)
plt.imshow(img, cmap='gray')
plt.axis('off')
plt.show()



In [54]:
blurred_image = ndimage.gaussian_filter(img, sigma=8)

In [55]:
plt.imshow(blurred_face, cmap='gray')
plt.axis('off')
plt.show()



In [63]:
face = scipy.misc.face(gray=True)
plt.imshow(face, cmap='gray')
plt.axis('off')
plt.show()



In [64]:
blurred_face = ndimage.gaussian_filter(face, sigma=5)
plt.imshow(blurred_face, cmap='gray')
plt.axis('off')
plt.show()



In [ ]: